package com.wsdcd.zlg.controller;

import com.wsdcd.zlg.core.exception.ERPException;
import com.wsdcd.zlg.model.entity.Order;
import com.wsdcd.zlg.model.service.OrderService;
import com.wsdcd.zlg.util.AjaxObject;
import com.wsdcd.zlg.util.PageParameter;
import com.wsdcd.zlg.util.PagerUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@AllArgsConstructor
@Controller
@RequestMapping("/order")
public class OrderController {
	private final OrderService orderService;
	private final HttpServletRequest request;

	@RequestMapping("list")
	public String login() {
		return "order/list";
	}

	@ResponseBody
	@RequestMapping("lists")
	public PagerUtil<Order> orderLists(int page,int limit,String searchType,String searchValue) {
		if(searchValue != null){
			searchValue = searchValue.trim();
		}
		PageParameter pageParameter = new PageParameter(page,limit,searchType,searchValue);
		return orderService.selectByPage(pageParameter);
	}
	
	@RequestMapping("add")
	public String add()
			throws ServletException, IOException {
		return "order/add";
	}

	@ResponseBody
	@RequestMapping("adds")
	public AjaxObject adds() {
		String orderUser = request.getParameter("customerNameType");
		String orderProducts = request.getParameter("orderProducts");
		String orderQuantityString = request.getParameter("orderQuantity");
		if(orderQuantityString == null || orderQuantityString.trim().equals("")){
			String msg = "没有填写订购数量";
			return new AjaxObject(0, msg);
		}
//		assert orderQuantityString != null;
		int orderQuantity = Integer.parseInt(orderQuantityString);
		
		String unitPriceBeforeTaxString = request.getParameter("unitPriceBeforeTax");
		if(orderQuantityString.trim().equals("")){
			String msg = "没有填写税前单价";
			return new AjaxObject(0, msg);
		}
		double unitPriceBeforeTax = Double.parseDouble(unitPriceBeforeTaxString);
		String unit = request.getParameter("unit");
		String state = request.getParameter("state");
		String time1 = request.getParameter("subscriptionDate");
		String time2 = request.getParameter("requiredDate");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date subscriptionDate = null;
		Date requiredDate = null;
		try {
			subscriptionDate = sdf.parse(time1);
			requiredDate = sdf.parse(time2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String orderRequirements = request.getParameter("orderRequirements");
		String relatedPictures = request.getParameter("relatedPictures");
		String orderAttachment = request.getParameter("orderAttachment");
		
		Order order = new Order(orderUser, orderProducts, orderQuantity, unitPriceBeforeTax, unit, state, subscriptionDate, requiredDate, orderRequirements, relatedPictures, orderAttachment);
		
		try {
			orderService.insert(order);
			String msg = "添加成功";
			return new AjaxObject(1, msg);
		} catch (ERPException e) {
			String msg = e.getMessage();
			return new AjaxObject(0, msg);
		}
	}
	
	@RequestMapping("update")
	public String update()
			throws ServletException, IOException {
		String orderId = request.getParameter("orderId");
		Order order = orderService.selectByOrderId(orderId);
		request.setAttribute("updateOrder", order);
		return "order/update";
	}

	@ResponseBody
	@RequestMapping("updates")
	public AjaxObject updates() {
		String orderIdString = request.getParameter("orderId");
		int orderId = Integer.parseInt(orderIdString);
		String orderUser = request.getParameter("orderUser");
		String orderProducts = request.getParameter("orderProducts");
		String orderQuantityString = request.getParameter("orderQuantity");
		if(orderQuantityString == null || orderQuantityString.trim().equals("")){
			String msg = "没有填写订购数量";
			return new AjaxObject(0, msg);
		}
		int orderQuantity = Integer.parseInt(orderQuantityString);
		
		String unitPriceBeforeTaxString = request.getParameter("unitPriceBeforeTax");
		if(orderQuantityString.trim().equals("")){
			String msg = "没有填写税前单价";
			return new AjaxObject(0, msg);
		}
		double unitPriceBeforeTax = Double.parseDouble(unitPriceBeforeTaxString);
		String unit = request.getParameter("unit");
		String state = request.getParameter("state");
		String time1 = request.getParameter("subscriptionDate");
		String time2 = request.getParameter("requiredDate");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date subscriptionDate = null;
		Date requiredDate = null;
		try {
			subscriptionDate = sdf.parse(time1);
			requiredDate = sdf.parse(time2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String orderRequirements = request.getParameter("orderRequirements");
		String relatedPictures = request.getParameter("relatedPictures");
		String orderAttachment = request.getParameter("orderAttachment");
		
		Order order = new Order(orderId, orderUser, orderProducts, orderQuantity, unitPriceBeforeTax, unit, state, subscriptionDate, requiredDate, orderRequirements, relatedPictures, orderAttachment);
		try {
			orderService.update(order);
			String msg = "修改成功";
			return new AjaxObject(1, msg);
		} catch (ERPException e) {
			String msg = e.getMessage();
			return new AjaxObject(0, msg);
		}
	}

	@ResponseBody
	@RequestMapping("deleteAll")
	public AjaxObject deleteAll() throws ServletException, IOException {
		String orderIds = request.getParameter("orderIds");
		if(orderIds == null || orderIds.trim().equals("")){
			String msg = "删除失败，请传递数据";
			return new AjaxObject(0, msg);
		}
		String[] arr = orderIds.split(",");
		int[] array = Arrays.stream(arr).mapToInt(Integer::parseInt).toArray();
		try {
			orderService.deleteAll(array);
			String msg = "删除成功";
			return new AjaxObject(1, msg);
		} catch (ERPException e) {
			String msg = e.getMessage();
			return new AjaxObject(0, msg);
		}
	}

}
